using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using JGSY.CMS.LowCode.Platform.Infrastructure.Middleware;
using JGSY.CMS.LowCode.Platform.Infrastructure.Configuration.ServiceModules;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Configuration.MiddlewareModules.Modules
{
    /// <summary>
    /// 核心中间件模块
    /// 包含异常处理、租户解析等核心中间件
    /// </summary>
    public class CoreMiddlewareModule : IMiddlewareModule
    {
        public string ModuleName => "核心中间件";
        public int Priority => 1; // 最高优先级
        public bool EnableInDevelopment => true;
        public bool EnableInProduction => true;
        public bool EnableInFastMode => true;
        public ModulePermission RequiredPermission => ModulePermission.CoreServices;
        public bool IsCriticalModule => true; // 关键中间件

        public void ConfigureMiddleware(WebApplication app, IConfiguration configuration)
        {
            // 全局异常处理中间件（必须是第一个）
            app.UseMiddleware<ExceptionHandlingMiddleware>();

            // 租户解析中间件（多租户支持）
            app.UseMiddleware<TenantResolutionMiddleware>();
        }

        public void ConfigureMinimalMiddleware(WebApplication app, IConfiguration configuration)
        {
            // 精简核心中间件 - 只配置异常处理
            app.UseMiddleware<ExceptionHandlingMiddleware>();
        }

        public bool ValidateConfiguration(IConfiguration configuration)
        {
            // 核心中间件无特殊配置要求
            return true;
        }
    }
}
